package com.chenjj.java.rmi.naming;

import com.chenjj.java.business.order.OrderInfoVO;
import com.chenjj.java.rmi.registry.RegistryService;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

public class NamingService {

    public static void main(String[] args) {
        try {
            // 本地主机上的远程对象注册表Registry的实例
            LocateRegistry.createRegistry(1100);
            // 创建一个远程对象
            HelloNamingFacade hello = new HelloNamingFacadeImpl();
            // 把远程对象注册到RMI注册服务器上，并命名为Hello
            //绑定的URL标准格式为：rmi://host:port/name
            Naming.bind("rmi://127.0.0.1:1100/HelloNaming", hello);
            System.out.println("======= 启动RMI服务成功! =======");
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            e.printStackTrace();
        }
    }

    interface HelloNamingFacade extends Remote {
        String helloWorld(String name, OrderInfoVO vo) throws RemoteException;
    }


    static class  HelloNamingFacadeImpl extends UnicastRemoteObject  implements  HelloNamingFacade{
        protected HelloNamingFacadeImpl() throws RemoteException {
            super();
        }

        @Override
        public String helloWorld(String name, OrderInfoVO vo) throws RemoteException {
            return "[Naming] 你好! " + name + "，交易金额:"+ vo.getMoney();
        }
    }

}



